package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.LinkMan;
import com.cz.nongyetongv5.bean.Town;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;

public class LianXiRenDetail extends Activity implements OnClickListener {

	/**
	 * 联系人的信息对象
	 */
	private LinkMan linkMan = null;

	/**
	 * 联系人的名字
	 */
	private TextView tvLinkManName = null;

	/**
	 * 电话的布局
	 */
	private ViewGroup rlMobileLayout = null;

	/**
	 * 手机号码的布局
	 */
	private ViewGroup rlTelLayout = null;

	/**
	 * 发送短信的布局
	 */
	private ViewGroup llMessageLayout = null;
	/**
	 * 分割线
	 */
	private View vSplit = null;

	/**
	 * 返回按钮
	 */
	private ViewGroup btnBack = null;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		// TODO Auto-generated method stub
		super.onDestroy();
	}
	private DatabaseHelper db = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 查询出数据库对象
		db = new DatabaseHelper(this, CommonConstants.DBNAME, null,
				CommonConstants.DATABASE_VERSION);
		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.tongxunlu_detail);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		tvLinkManName = (TextView) findViewById(R.id.tvLinkManName);

		rlMobileLayout = (ViewGroup) findViewById(R.id.rlMobileLayout);

		rlMobileLayout.setOnClickListener(this);

		vSplit = findViewById(R.id.vSplit);

		rlTelLayout = (ViewGroup) findViewById(R.id.rlTelLayout);
		rlTelLayout.setOnClickListener(this);

		llMessageLayout = (ViewGroup) findViewById(R.id.llMessageLayout);
		llMessageLayout.setOnClickListener(this);

		Intent intent = getIntent();

		linkMan = (LinkMan) intent.getSerializableExtra("linkMan");

		String location = intent.getStringExtra("location");

		if (linkMan != null) {

			String realName = null;
			if (CharsetUtil.isNotNull(linkMan.getRealName())) {

				realName = linkMan.getRealName();

			} else {
				realName = "未知";
			}

			tvLinkManName.setText(realName);

		}

		TextView tvTel = (TextView) findViewById(R.id.tvTel);

		TextView tvMobile = (TextView) findViewById(R.id.tvMobile);

		if (CharsetUtil.isNotNull(linkMan.getMobile())
				&& CharsetUtil.isNotNull(linkMan.getTel())) {
			vSplit.setVisibility(View.VISIBLE);
			rlMobileLayout.setVisibility(View.VISIBLE);
			rlTelLayout.setVisibility(View.VISIBLE);

			tvMobile.setText("联系电话\u0020:\u0020" + linkMan.getTel());

			tvTel.setText("手机号码\u0020:\u0020" + linkMan.getMobile());

		} else {
			vSplit.setVisibility(View.GONE);

			if (CharsetUtil.isNotNull(linkMan.getTel())) {
				tvMobile.setText("联系电话\u0020:\u0020" + linkMan.getTel());

				rlMobileLayout.setVisibility(View.VISIBLE);
				rlTelLayout.setVisibility(View.GONE);

			} else if (CharsetUtil.isNotNull(linkMan.getMobile())) {
				tvTel.setText("手机号码\u0020:\u0020" + linkMan.getMobile());
				rlMobileLayout.setVisibility(View.GONE);
				rlTelLayout.setVisibility(View.VISIBLE);
			}

		}

		TextView tvRank = (TextView) findViewById(R.id.tvRank);

		String rank = null;

		if (linkMan.getRank() == 1) {
			rank = "内检员";
		} else if (linkMan.getRank() == 2) {
			rank = "协管员";
		} else if (linkMan.getRank() == 3) {
			rank = "监管员";
		}

		tvRank.setText("监管类别\u0020:\u0020" + rank);

		TextView tvSex = (TextView) findViewById(R.id.tvSex);

		if (CharsetUtil.isNotNull(linkMan.getSexStr())) {

			tvSex.setText("性别\u0020:\u0020" + linkMan.getSexStr());
		} else {
			tvSex.setText("性别\u0020:\u0020未知");

		}

		TextView tvBuMen = (TextView) findViewById(R.id.tvBuMen);

		if (CharsetUtil.isNotNull(linkMan.getDepartmentName())) {

			tvBuMen.setText("工作单位\u0020:\u0020" + linkMan.getDepartmentName());
		} else {
			tvBuMen.setText("工作单位\u0020:\u0020未知");

		}

		TextView tvLocation = (TextView) findViewById(R.id.tvLocation);




		LinkMan ma=db.queryLinkMan(linkMan.getId());
		Log.d("rafelinkman",ma.getRegionCode());
		Town town=db.queryTownById(ma.getRegionCode());
		if (town==null) {
			tvLocation.setText("所属区域\u0020:\u0020未知");


		} else {


			String pathStr=town.getName();
			int sec=1;
			while(town!=null&&sec<100){
				town=db.queryTownById(town.getParent_code());
				if(town!=null)
					pathStr=town.getName()+">>"+pathStr;
				sec++;

			}tvLocation.setText("所属区域\u0020:\u0020" + pathStr);
		}

		//
		//		if (CharsetUtil.isNotNull(location)) {
		//
		//			if (location.endsWith(">>")) {
		//
		//				location = location.substring(0, location.length() - 2);
		//			}
		//
		//			tvLocation.setText("所属区域\u0020:\u0020" + location);
		//		} else {
		//			tvLocation.setText("所属区域\u0020:\u0020未知");
		//
		//		}

		TextView tvPost = (TextView) findViewById(R.id.tvPost);

		if (CharsetUtil.isNotNull(linkMan.getPost())) {

			tvPost.setText("职务\u0020:\u0020" + linkMan.getPost());
		} else {
			tvPost.setText("职务\u0020:\u0020未知");

		}

		loadRightTitle();

	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	@Override
	public void onClick(View v) {

		// 拨打电话
		if (v == rlMobileLayout) {
			Intent intent_to_call = new Intent(Intent.ACTION_DIAL,
					Uri.parse("tel:" + linkMan.getTel()));

			startActivity(intent_to_call);

		}

		// 拨打手机
		if (v == rlTelLayout) {
			Intent intent_to_call = new Intent(Intent.ACTION_DIAL,
					Uri.parse("tel:" + linkMan.getMobile()));

			startActivity(intent_to_call);
		}

		// 发送短信
		if (v == llMessageLayout) {
			Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"
					+ linkMan.getMobile()));
			startActivity(intent);
		}

		// 退出
		if (v == btnBack) {

			finish();
		}

	}

}
